<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    {{dataForm}}
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="规格名称" prop="specName">
        <el-input v-model="dataForm.specName" placeholder="规格名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" plain size="small" @click="dataForm.options.push({})">新增规格选项</el-button>
      </el-form-item>
      <el-form-item>
        <el-table
          :data="dataForm.options"
          style="width: 100%">
          <el-table-column
            label="规格选项"
            width="250">
            <template slot-scope="scope">
             <el-input v-model="scope.row.optionName"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="排序"
            width="250">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orders"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          specName: '',
          options: []
        },
        dataRule: {
          specName: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                // this.dataForm.specName = data.specification.specName
                this.dataForm = data.specification;
              }
            })
          }
        })
      },
      handleDelete(index) {
        console.log(index);
        this.dataForm.options.splice(index,1);    //splice(参数1，参数2) 参数1：代表要删除的元素在数组中下标位置，参数2：删除的次数
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${this.dataForm.id ? 'PUT' : 'POST'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'specName': this.dataForm.specName,
                'options': this.dataForm.options
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
